package com.alibaba.citrus.ots.unified.inventory.facade.service;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.unified.inventory.functions.consignmentOrder.ConsignmentOrderEntity;
import com.alibaba.citrus.ots.unified.inventory.functions.consignmentOrder.ConsignmentOrderFactory;
import com.alibaba.citrus.ots.unified.inventory.repository.ConsignmentOrderRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.middleware.mq.runtime.EpochMessageService;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsunifiedinventory.api.consignmentorderwrite.service.ConsignmentOrderWriteService;
import com.epoch.app.otsunifiedinventory.dto.request.ConsignmentOrderWriteCreateRequest;
import com.epoch.app.otsunifiedinventory.model.dto.ConsignmentOrderCreateResponse;
import com.epoch.app.otsunifiedinventory.model.dto.ConsignmentOrderWriteUpdateRequest;
import com.epoch.app.otsunifiedinventory.sdo.ConsignmentOrderSDO;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Primary
public class ConsignmentOrderWriteServiceImpl implements ConsignmentOrderWriteService {

    @Resource
    private ConsignmentOrderRepository consignmentOrderRepository;
    @Resource
    private  EpochMessageService messageService;

    private final Log log = Log.getLogger(ConsignmentOrderWriteServiceImpl.class);
    @Override
    @FacadeInvoker
    public Result<ConsignmentOrderCreateResponse> create(ConsignmentOrderWriteCreateRequest consignmentOrderWriteCreateRequest) {

        ConsignmentOrderSDO query = consignmentOrderRepository.query(consignmentOrderWriteCreateRequest.getOuterDeliveryOrderId());
        if (query != null && Objects.equals(query.getStatus(),0)){
            log.error("ConsignmentOrderWriteServiceImpl_order_isDeleted:{}",JSON.toJSONString(consignmentOrderWriteCreateRequest));
            return Result.success(new ConsignmentOrderCreateResponse());
        }
        if (query != null) {
            consignmentOrderRepository.deleteOrder(query);
        }
        ConsignmentOrderEntity consignmentOrderEntity = ConsignmentOrderFactory.createConsignmentOrder(consignmentOrderWriteCreateRequest);
        String s = consignmentOrderEntity.create();
        ConsignmentOrderCreateResponse response = new ConsignmentOrderCreateResponse();
        response.setId(s);
        //发送创建消息消息，订单接收
        try {
            messageService.sendMessage("consignment_order_message", "CR_CONSIGNMENT", "ConsignmentCreated", consignmentOrderEntity.getConsignmentOrderSDO().getOuterDeliveryOrderId(), JSON.toJSONString(consignmentOrderEntity.getConsignmentOrderSDO()));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("发送交货单创建消息失败！",e);
        }
        return Result.success(response);
    }

    @Override
    public Result<Boolean> deleteBySapDeliveryOrder(ConsignmentOrderWriteUpdateRequest consignmentOrderWriteUpdateRequest) {
        ConsignmentOrderSDO query = consignmentOrderRepository.query(consignmentOrderWriteUpdateRequest.getOuterDeliveryOrderId());
        if(query == null){
            log.info("consignmentOrder is not existed "+ consignmentOrderWriteUpdateRequest.getOuterDeliveryOrderId());
            return Result.success(true);
        }
        ConsignmentOrderEntity consignmentOrderEntity = ConsignmentOrderFactory.deleteConsignmentOrder(query);
        consignmentOrderEntity.delete();
        //发送删除消息，订单接收
        try {
            messageService.sendMessage("consignment_order_message", "CR_CONSIGNMENT", "ConsignmentDeleted", consignmentOrderEntity.getConsignmentOrderSDO().getId(), JSON.toJSONString(consignmentOrderEntity.getConsignmentOrderSDO()));
        } catch (Exception e) {
            log.error("发送交货单删除消息失败！",e);
        }
        return Result.success(true);
    }
}
